//
//  ViewController.m
//  UI10-易车
//
//  Created by 韩旭 on 16/7/27.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "HX_TableViewCell.h"
#import "HX_TableViewDetail.h"
#import "Model.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) NSMutableArray *arrCycleImage;
@property (nonatomic, strong) NSMutableArray *arrList;

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr = [NSMutableArray array];
    _arrCycleImage = [NSMutableArray array];
    _arrList = [NSMutableArray array];
    self.navigationItem.title = @"易车";
    self.view.backgroundColor = [UIColor whiteColor];
    self.arr = [[Model arrGroup] mutableCopy];
    for (Model *model in _arr) {
        for (Model *modelCycle in model.arrCycleImage) {
            [_arrCycleImage addObject:modelCycle.picCover];
        }
    }
    [self creatTableView];
    [self creatScrollView];
    _tableView.tableHeaderView = _scrollView;
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[HX_TableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView registerClass:[HX_TableViewDetail class] forCellReuseIdentifier:@"cell1"];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell2"];
}
- (void)creatScrollView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, HEIGHT * 0.2)];
    for (int i = 0; i < _arrCycleImage.count; i++) {
        UIImageView *imageCycle = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth * i, 0, kWidth, kHeight * 0.2)];
        [imageCycle sd_setImageWithURL:[NSURL URLWithString:_arrCycleImage[i]] placeholderImage:[UIImage imageNamed:@"020035625"]];
        [self.scrollView addSubview:imageCycle];
    }
    self.scrollView.pagingEnabled = YES;
    _scrollView.tag = 1000;
    self.scrollView.backgroundColor = COLORAUTO;
    _scrollView.contentSize = CGSizeMake(kWidth * _arrCycleImage.count, 0);
    [self.view addSubview:_scrollView];
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changePic) userInfo:nil repeats:YES];
    
}
- (void)changePic {
    UIScrollView *scroll = (UIScrollView *)[self.view viewWithTag:1000];
    [scroll setContentOffset:CGPointMake(scroll.contentOffset.x + self.view.frame.size.width, 0) animated:YES];
    // 进行偏移量的判断, 做出循环
    if (scroll.contentOffset.x == self.view.frame.size.width * 3) {
        scroll.contentOffset = CGPointZero;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    Model *model = self.arr[section];
    return model.arrayList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        HX_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else if (indexPath.row == 10) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell.backgroundColor = [UIColor yellowColor];
        return cell;
    } else {
        HX_TableViewDetail *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.backgroundColor = [UIColor clearColor];
        Model *model = self.arr[indexPath.section];
        cell.lableDetail.text = [model.arrayList[indexPath.row] title];
        NSString *string = [model.arrayList[indexPath.row] picCover];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"020035625"]];
        return cell;
    }
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 100;
    }
    return 80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
